/**
* 显示消息提示框
* @param content 提示的标题
*/
export function toast(content) {
  uni.showToast({
    icon: 'none',
    title: content
  })
}

/**
* 显示消息提示框
* @param content 提示的标题
*/
export function titleToast(content) {
  uni.showActionSheet({
    itemList: content,
	success: function(res) {
		uni.closeLoading()	
	}
  })
}

/**
* 显示模态弹窗
* @param content 提示的标题
*/
export function showConfirm(content) {
  return new Promise((resolve, reject) => {
    uni.showModal({
      title: '提示',
      content: content,
      cancelText: '取消',
      confirmText: '确定',
      success: function(res) {
        resolve(res)
      }
    })
  })
}

/**
* 参数处理
* @param params 参数
*/
export function tansParams(params) {
  let result = ''
  for (const propName of Object.keys(params)) {
    const value = params[propName]
    var part = encodeURIComponent(propName) + "="
    if (value !== null && value !== "" && typeof (value) !== "undefined") {
      if (typeof value === 'object') {
        for (const key of Object.keys(value)) {
          if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
            let params = propName + '[' + key + ']'
            var subPart = encodeURIComponent(params) + "="
            result += subPart + encodeURIComponent(value[key]) + "&"
          }
        }
      } else {
        result += part + encodeURIComponent(value) + "&"
      }
    }
  }
  return result
}

/**
 * 处理图片地址
 * @param {string} url 图片地址
 * @returns 图片地址
 */
export function mmvImageUrl(url) {
  if (uni.$u.test.isEmpty(url)) {
    // return url;
    return 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2Ffbf18a5314f750da671711dfb176cf8791fbc687153d-g7YSBF_fw658&refer=http%3A%2F%2Fhbimg.b0.upaiyun.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1648359683&t=f1f85165dfcd41fa2183e748ea2e458d';
  }
  if (uni.$u.test.url(url)) {
    return url;
  }
  if (uni.$u.test.contains(url, 'static/')) {
    return url;
  }
  return SETTINGS.baseImageUrl + url;
}


export function toParams(param) {
  console.log('toParams', param)
  var result = ""
  for (let name in param) {
    if (typeof param[name] != 'function') {
      result += "&" + name + "=" + encodeURI(param[name]);
    }
  }
  return result.substring(1)
}

export function validData(rule, value, callback) {
  // console.log('aaa', rule, value, callback)
  if (rule.required && !value) {
    // this.$modal.msgError(`未填写${value}`);
    callback(new Error(rule.message));
  } else {
    callback();
  }
}

export function isEmpty(val) {
  if(val === undefined || val === null || val === ''){
    return true;
  }
  if(Array.isArray(val) && val.length<=0){
    return true;
  }

  if(val.constructor === Object && Object.keys(val).length === 0){
    return true;
  }

  return false;
}
